package com.example.maple.ccsclient.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.model.PicModel;
import com.example.maple.ccsclient.utils.DisplayUtils;
import com.example.maple.ccsclient.view.OnItemClickListener;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by maple on 2016/8/16.
 */
public class AlbumsRecyclerViewAdapter extends RecyclerView.Adapter<AlbumsRecyclerViewAdapter.ViewHolder> {

	List<String> mLists;
	int mResource;

	Context mContext;
	//最大的列数
	public static final int MAX_COLUMN = 3;

	public AlbumsRecyclerViewAdapter(Context context, int resource, List<String> list) {
		mContext = context;
		mResource = resource;
		mLists = list;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

		// 加载数据item的布局，生成VH返回
		View v = LayoutInflater.from(mContext).inflate(mResource, null);
		// 根据列数计算项目宽度，以使总宽度尽量填充屏幕
		int itemWidth = (mContext.getResources().getDisplayMetrics().widthPixels - (MAX_COLUMN + 1) * DisplayUtils.dip2px(mContext, 2) - DisplayUtils.dip2px(mContext, 16)) / MAX_COLUMN;
		// 下面根据比例计算您的item的高度，此处只是使用itemWidth
		int itemHeight = itemWidth;
		AbsListView.LayoutParams param = new AbsListView.LayoutParams(
				itemWidth,
				itemHeight);
		v.setLayoutParams(param);
		return new ViewHolder(v);


	}

	private OnItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.mOnItemClickListener = onItemClickListener;
	}

	//定义当前GridView是否在滑动状态
	private boolean scrollState = false;

	public void setScrollState(boolean scrollState) {
		this.scrollState = scrollState;
	}

	@Override
	public void onBindViewHolder(final ViewHolder holder, int position) {
		String info = mLists.get(position);
		if (!TextUtils.isEmpty(info)) {

			if (!scrollState) {
				holder.mSimpleDraweeView.setImageURI(Uri.parse(info));
				if (mOnItemClickListener != null) {
					holder.mCheckBox.setOnCheckedChangeListener(
							new CompoundButton.OnCheckedChangeListener() {
								@Override
								public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
									holder.mCheckBox.setChecked(isChecked);
									if(isChecked){
										holder.mCheckBox.setChecked(isChecked);
										holder.mView.setVisibility(View.VISIBLE);
									}else {
										holder.mView.setVisibility(View.INVISIBLE);
									}
								}
							}
					);
				}
			} else {
				holder.mSimpleDraweeView.setImageURI(Uri.parse("http://img.blog.csdn.net/20151015163826688"));
			}

		}

	}

	@Override
	public int getItemCount() {
		return mLists.size();
	}


	public class ViewHolder extends RecyclerView.ViewHolder {

		View parent;
		SimpleDraweeView mSimpleDraweeView;
		View mView;
		CheckBox mCheckBox;

		public ViewHolder(View itemView) {
			super(itemView);
			parent = itemView;
			mSimpleDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.image);
			mView = itemView.findViewById(R.id.globe);
			mCheckBox = (CheckBox) itemView.findViewById(R.id.checkbox);
		}
	}
}
